package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.DayMenu;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthMenu;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MonthMenuStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MonthMenuMapper monthMenuMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void monthStatistics(String oId) {

        int mId = timeUtil.getMonthTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            MonthMenu monthMenu = monthMenuMapper.selectMonthMenuFromDIdAndMId(mId, menuOrderList.get(i).getMId());
            if(monthMenu == null) {
                monthMenu = new MonthMenu();
                monthMenu.setMmId(monthMenuMapper.selectCount(null) + 1);
                monthMenu.setMonId(mId);
                monthMenu.setMId(menuOrderList.get(i).getMId());
                monthMenu.setMName(menuOrderList.get(i).getMName());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthMenu.setMmIncome(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                monthMenu.setMmExpand(menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = monthMenu.getMmIncome() - monthMenu.getMmExpand();
                monthMenu.setMmProfit(profit);
                monthMenu.setMmNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(monthMenu));
                monthMenuMapper.insert(monthMenu);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthMenu.setMonId(monthMenu.getMonId());
                monthMenu.setMName(menuOrderList.get(i).getMName());
                monthMenu.setMmIncome(monthMenu.getMmIncome() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                monthMenu.setMmExpand(monthMenu.getMmExpand() + menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                double profit = (menu.getMPrice() * menuOrderList.get(i).getMoNum()) - (menu.getMOldPrice() * menuOrderList.get(i).getMoNum());
                monthMenu.setMmProfit(monthMenu.getMmProfit() + profit);
                monthMenu.setMmNum(monthMenu.getMmNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(monthMenu));
                monthMenuMapper.updateById(monthMenu);
            }
        }
    }

}
